<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Git 备忘清单
 &#x26;  git cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="本备忘单总结了常用的 Git 命令行指令，以供快速参考。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="git,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/git.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="git-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16" height="1em" width="1em">
  <path d="M15.698 7.287 8.712.302a1.03 1.03 0 0 0-1.457 0l-1.45 1.45 1.84 1.84a1.223 1.223 0 0 1 1.55 1.56l1.773 1.774a1.224 1.224 0 0 1 1.267 2.025 1.226 1.226 0 0 1-2.002-1.334L8.58 5.963v4.353a1.226 1.226 0 1 1-1.008-.036V5.887a1.226 1.226 0 0 1-.666-1.608L5.093 2.465l-4.79 4.79a1.03 1.03 0 0 0 0 1.457l6.986 6.986a1.03 1.03 0 0 0 1.457 0l6.953-6.953a1.031 1.031 0 0 0 0-1.457"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#git-备忘清单"><span class="icon icon-link"></span></a>Git 备忘清单</h1><div class="wrap-body">
<p>本备忘单总结了常用的 <a href="https://git-scm.com/">Git</a> 命令行指令，以供快速参考。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建存储库">创建存储库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#做出改变">做出改变</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置">配置</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用分支">使用分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#临时提交">临时提交</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#观察你的存储库">观察你的存储库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#忽略文件-gitignore">忽略文件 .gitignore</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重构文件名">重构文件名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#同步">同步</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#远程">远程</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#跟踪路径更改">跟踪路径更改</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#git-配置-ssh-代理">git 配置 ssh 代理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#gitattributes">.gitattributes</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#git-技巧">Git 技巧</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重命名分支">重命名分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#log">Log</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#分支">分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#commit">Commit</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#忽略文件的权限变化">忽略文件的权限变化</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#git-别名">Git 别名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#设置大小写敏感">设置大小写敏感</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改远程-commit-记录">修改远程 Commit 记录</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#撤销远程记录">撤销远程记录</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#放弃本地修改内容">放弃本地修改内容</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取最近一次提交的-hash">获取最近一次提交的 Hash</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除已经合并到-master-的分支">删除已经合并到 master 的分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#把-a-分支的某一个-commit放到-b-分支上">把 A 分支的某一个 commit，放到 B 分支上</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#回到远程仓库的状态">回到远程仓库的状态</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重设第一个-commit">重设第一个 commit</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看冲突文件列表">查看冲突文件列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示工作区的冲突文件列表">展示工作区的冲突文件列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示暂存区和最近版本的不同">展示暂存区和最近版本的不同</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#中文乱码的解决方案">中文乱码的解决方案</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示暂存区工作区和最近版本的不同">展示暂存区、工作区和最近版本的不同</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除已经合并到-master-的分支-1">删除已经合并到 master 的分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关联远程分支">关联远程分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看远程分支和本地分支的对应关系">查看远程分支和本地分支的对应关系</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示当前分支的最近的-tag">展示当前分支的最近的 tag</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看某段代码是谁写的">查看某段代码是谁写的</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改作者名">修改作者名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改远程仓库的-url">修改远程仓库的 url</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#增加远程仓库">增加远程仓库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列出所有远程仓库">列出所有远程仓库</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看两个星期内的改动">查看两个星期内的改动</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从-stash-中拿出某个文件的修改">从 stash 中拿出某个文件的修改</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示所有-tracked-的文件">展示所有 tracked 的文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示所有-untracked-的文件">展示所有 untracked 的文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示所有忽略的文件">展示所有忽略的文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#把某一个分支导出成一个文件">把某一个分支导出成一个文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从包中导入分支">从包中导入分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#执行-rebase-之前自动-stash">执行 rebase 之前自动 stash</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从远程仓库根据-id拉下某一状态到本地分支">从远程仓库根据 ID，拉下某一状态，到本地分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#详细展示一行中的修改">详细展示一行中的修改</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#清除-gitignore-文件中记录的文件">清除 gitignore 文件中记录的文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示忽略的文件">展示忽略的文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#commit-历史中显示-branch1-有的但是-branch2-没有-commit">commit 历史中显示 Branch1 有的但是 Branch2 没有 commit</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在-commit-log-中显示-gpg-签名">在 commit log 中显示 GPG 签名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#新建并切换到新分支上同时这个分支没有任何-commit">新建并切换到新分支上，同时这个分支没有任何 commit</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#展示任意分支某一文件的内容">展示任意分支某一文件的内容</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置-http-和-socks-代理">配置 http 和 socks 代理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#clone-最新一次提交">clone 最新一次提交</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#忽略某个文件的改动">忽略某个文件的改动</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#以最后提交的顺序列出所有-git-分支">以最后提交的顺序列出所有 Git 分支</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在-commit-log-中查找相关内容">在 commit log 中查找相关内容</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#把暂存区的指定-file-放到工作区中">把暂存区的指定 file 放到工作区中</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置-ssh-协议代理">配置 SSH 协议代理</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#conventional-commmits">Conventional Commmits</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#格式">格式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用">常用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#commit-类型">Commit 类型</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建存储库"><a aria-hidden="true" tabindex="-1" href="#创建存储库"><span class="icon icon-link"></span></a>创建存储库</h3><div class="wrap-body">
<p>创建一个新的本地存储库</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> init <span class="token punctuation">[</span>项目名称<span class="token punctuation">]</span>
</span></code></pre>
<p>克隆存储库(代码仓库)</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> clone <span class="token operator">&#x3C;</span>git_url<span class="token operator">></span>
</span></code></pre>
<p>将存储库克隆到指定目录</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> clone <span class="token operator">&#x3C;</span>git_url<span class="token operator">></span> 指定目录
</span></code></pre>
<p>将存储库克隆到指定目录，并指定分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> clone <span class="token operator">&#x3C;</span>git_url<span class="token operator">></span> <span class="token parameter variable">-b</span> <span class="token operator">&#x3C;</span>分支名称<span class="token operator">></span> 指定目录
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="做出改变"><a aria-hidden="true" tabindex="-1" href="#做出改变"><span class="icon icon-link"></span></a>做出改变</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<p>在工作目录中<strong>显示</strong>修改后的文件，为您的下一次提交暂存</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> status
</span></code></pre>
<p>暂存文件，准备提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">add</span> <span class="token punctuation">[</span>file<span class="token punctuation">]</span>
</span></code></pre>
<p>暂存<strong>所有</strong>更改的文件，准备提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span>
</span></code></pre>
<p>将所有暂存文件提交到版本化历史记录</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> commit <span class="token parameter variable">-m</span> <span class="token string">"commit message"</span>
</span></code></pre>
<p>将所有跟踪的文件提交到版本化历史记录</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> commit <span class="token parameter variable">-am</span> <span class="token string">"commit message"</span>
</span></code></pre>
<p>取消暂存文件，保留文件更改</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> reset <span class="token punctuation">[</span>file<span class="token punctuation">]</span>
</span></code></pre>
<p>将所有内容恢复到最后一次提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> reset <span class="token parameter variable">--hard</span>
</span></code></pre>
<p>已更改但未暂存内容的差异</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span>
</span></code></pre>
<p>已 commited 但尚未提交的内容的差异</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> <span class="token parameter variable">--staged</span>
</span></code></pre>
<p>在指定分支之前应用当前分支的任何提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> rebase <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="配置"><a aria-hidden="true" tabindex="-1" href="#配置"><span class="icon icon-link"></span></a>配置</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>设置将附加到您的提交和标签的名称</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> user.name <span class="token string">"name"</span>
</span></code></pre>
<p>设置将附加到您的提交和标签 tags 的<strong>电子邮件地址</strong></p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> user.email <span class="token string">"email"</span>
</span></code></pre>
<p>启用 Git 输出的一些着色</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> color.ui auto
</span></code></pre>
<p>在文本编辑器中编辑全局配置文件</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--edit</span>
</span></code></pre>
<p>显示本地 <code>repo</code> 配置设置</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--list</span>
</span></code></pre>
<p>删除全局设置</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> <span class="token operator">&#x3C;</span>entry-name<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="使用分支"><a aria-hidden="true" tabindex="-1" href="#使用分支"><span class="icon icon-link"></span></a>使用分支</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<p>列出所有本地分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch
</span></code></pre>
<p>列出所有分支，本地和远程</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-av</span>
</span></code></pre>
<p>切换到 <code>my_branch</code>，并更新工作目录</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout my_branch
</span></code></pre>
<p>创建一个名为 <code>new_branch</code> 的新分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout <span class="token parameter variable">-b</span> new_branch
</span></code></pre>
<p>删除名为 <code>my_branch</code> 的分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-d</span> my_branch
</span></code></pre>
<p>将分支 <code>A</code> 合并到分支 <code>B</code></p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout branchB
</span><span class="code-line">$ <span class="token function">git</span> merge branchA
</span></code></pre>
<p>标记当前提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> tag my_tag
</span></code></pre>
<p>从远程分支中创建并切换到本地分支</p>
<pre class="wrap-text"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout <span class="token parameter variable">-b</span> <span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span> origin/<span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="临时提交"><a aria-hidden="true" tabindex="-1" href="#临时提交"><span class="icon icon-link"></span></a>临时提交</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 保存已修改和分阶段的更改</span>
</span><span class="code-line">$ <span class="token function">git</span> stash
</span><span class="code-line"><span class="token comment"># 列出隐藏文件更改的堆栈顺序</span>
</span><span class="code-line">$ <span class="token function">git</span> stash list
</span><span class="code-line"><span class="token comment"># 从存储堆栈顶部编写工作</span>
</span><span class="code-line">$ <span class="token function">git</span> stash pop
</span><span class="code-line"><span class="token comment"># 丢弃存储堆栈顶部的更改</span>
</span><span class="code-line">$ <span class="token function">git</span> stash drop
</span><span class="code-line"><span class="token comment"># 回到某个 stash 的状态</span>
</span><span class="code-line">$ <span class="token function">git</span> stash apply <span class="token operator">&#x3C;</span>stash@<span class="token punctuation">{</span>n<span class="token punctuation">}</span><span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 删除所有的 stash</span>
</span><span class="code-line">$ <span class="token function">git</span> stash <span class="token function">clear</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="观察你的存储库"><a aria-hidden="true" tabindex="-1" href="#观察你的存储库"><span class="icon icon-link"></span></a>观察你的存储库</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>显示当前活动分支的提交历史</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log
</span></code></pre>
<p>显示 branchA 上不在 branchB 上的提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log branchB<span class="token punctuation">..</span>branchA
</span></code></pre>
<p>显示更改文件的提交，即使跨重命名</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log <span class="token parameter variable">--follow</span> <span class="token punctuation">[</span>file<span class="token punctuation">]</span>
</span></code></pre>
<p>显示 branchA 中的内容与 branchB 中的内容的差异</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> branchB<span class="token punctuation">..</span>.branchA
</span></code></pre>
<p>以人类可读的格式显示 Git 中的任何对象</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> show <span class="token punctuation">[</span>SHA<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="忽略文件-gitignore"><a aria-hidden="true" tabindex="-1" href="#忽略文件-gitignore"><span class="icon icon-link"></span></a>忽略文件 .gitignore</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<p>文件 <code>.gitignore</code> 指定了 <code>Git</code> 应该忽略的 <strong>未跟踪的</strong> 文件</p>

























































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left">行首 <code>#</code></td><td align="left">全行注释，不支持行尾类注释 <em>(转义 <code>\#</code>)</em></td></tr><tr><td align="left">行首 <strong><code>!</code></strong></td><td align="left">否定模式 <em>(转义 <code>\!</code>)</em></td></tr><tr><td align="left"><code>**</code></td><td align="left">匹配任意路径</td></tr><tr><td align="left"><code>*</code></td><td align="left">匹配任意多个字符</td></tr><tr><td align="left"><code>?</code></td><td align="left">匹配任意一个字符</td></tr><tr><td align="left"><code>doc/**</code></td><td align="left">匹配 <code>doc</code> 文件夹下的全部内容</td></tr><tr><td align="left"><code>doc/**/a</code></td><td align="left">匹配任意深度路径下的 <code>a</code> 文件或文件夹</td></tr><tr><td align="left"><code>/</code></td><td align="left">表示路径分隔符，不区分操作系统</td></tr><tr><td align="left"><code>/</code> 结尾</td><td align="left">仅会匹配文件夹，否则会匹配文件和文件夹</td></tr><tr><td align="left">空行</td><td align="left">不匹配任何文件</td></tr><tr><td align="left">行尾空格</td><td align="left">默认被忽略，可使用<code>\</code>进行转义</td></tr><tr><td align="left">行首空格</td><td align="left">被正常处理，不会被忽略</td></tr></tbody></table>
<p>当前 <code>.gitignore</code> 文件定义规则的优先级高于上级路径 <code>.gitignore</code> 定义规则的优先级；后定义的规则优先级高于前面定义规则的优先级</p>
<pre class="wrap-text"><code class="language-gitignore code-highlight"><span class="code-line line-number" line="1"><span class="token comment"># 忽略当前目录logs文件夹下的全部内容</span>
</span><span class="code-line line-number" line="2"><span class="token entry string"><span class="token punctuation">/</span>logs<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="3"><span class="token entry string"><span class="token punctuation">/</span>logs<span class="token punctuation">/</span><span class="token operator">*</span></span>
</span><span class="code-line line-number" line="4"><span class="token entry string"><span class="token punctuation">/</span>logs<span class="token punctuation">/</span><span class="token operator">**</span></span>
</span><span class="code-line line-number" line="5"><span class="token comment"># 上述几条规则等效</span>
</span><span class="code-line line-number" line="6">
</span><span class="code-line line-number" line="7"><span class="token comment"># 忽略 Mac 系统文件，包括任意子路径下的同名文件（夹）</span>
</span><span class="code-line line-number" line="8"><span class="token entry string">.DS_store</span>
</span><span class="code-line line-number" line="9">
</span><span class="code-line line-number" line="10"><span class="token comment"># 忽略 node_modules 文件夹，包括任意子路径下的同名文件夹</span>
</span><span class="code-line line-number" line="11"><span class="token entry string">node_modules<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="12">
</span><span class="code-line line-number" line="13"><span class="token comment"># 忽略任意子路径下build、target文件夹，</span>
</span><span class="code-line line-number" line="14"><span class="token comment"># 但不忽略src/main、src/test下的build、target文件夹</span>
</span><span class="code-line line-number" line="15"><span class="token entry string">build<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="16"><span class="token entry string"><span class="token operator">!</span><span class="token operator">**</span><span class="token punctuation">/</span>src<span class="token punctuation">/</span>main<span class="token punctuation">/</span><span class="token operator">**</span><span class="token punctuation">/</span>build<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="17"><span class="token entry string"><span class="token operator">!</span><span class="token operator">**</span><span class="token punctuation">/</span>src<span class="token punctuation">/</span>test<span class="token punctuation">/</span><span class="token operator">**</span><span class="token punctuation">/</span>build<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="18"><span class="token entry string">target<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="19"><span class="token entry string"><span class="token operator">!</span><span class="token operator">**</span><span class="token punctuation">/</span>src<span class="token punctuation">/</span>main<span class="token punctuation">/</span><span class="token operator">**</span><span class="token punctuation">/</span>target<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="20"><span class="token entry string"><span class="token operator">!</span><span class="token operator">**</span><span class="token punctuation">/</span>src<span class="token punctuation">/</span>test<span class="token punctuation">/</span><span class="token operator">**</span><span class="token punctuation">/</span>target<span class="token punctuation">/</span></span>
</span><span class="code-line line-number" line="21">
</span><span class="code-line line-number" line="22"><span class="token comment"># 使用 ! 重新包含指定文件（夹）</span>
</span><span class="code-line line-number" line="23"><span class="token entry string"><span class="token operator">!</span>logs<span class="token punctuation">/</span>.gitkeep</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重构文件名"><a aria-hidden="true" tabindex="-1" href="#重构文件名"><span class="icon icon-link"></span></a>重构文件名</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 从工作目录中删除文件并暂存删除</span>
</span><span class="code-line"><span class="token function">git</span> <span class="token function">rm</span> <span class="token operator">&#x3C;</span>filename<span class="token operator">></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 从版本控制中删除文件但在本地保留文件</span>
</span><span class="code-line"><span class="token function">git</span> <span class="token function">rm</span> <span class="token parameter variable">--cached</span> <span class="token operator">&#x3C;</span>filename<span class="token operator">></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 更改文件名并准备提交</span>
</span><span class="code-line"><span class="token function">git</span> <span class="token function">mv</span> <span class="token operator">&#x3C;</span>filename-orig<span class="token operator">></span> <span class="token operator">&#x3C;</span>filename-renamed<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="同步"><a aria-hidden="true" tabindex="-1" href="#同步"><span class="icon icon-link"></span></a>同步</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>从该 Git 远程获取所有分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> fetch <span class="token punctuation">[</span>alias<span class="token punctuation">]</span>
</span></code></pre>
<p>将远程分支合并到当前分支以使其保持最新状态</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> merge <span class="token punctuation">[</span>alias<span class="token punctuation">]</span>/<span class="token punctuation">[</span>branch<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 没有快进</span>
</span><span class="code-line">$ <span class="token function">git</span> merge --no-ff <span class="token punctuation">[</span>alias<span class="token punctuation">]</span>/<span class="token punctuation">[</span>branch<span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 仅快进</span>
</span><span class="code-line">$ <span class="token function">git</span> merge --ff-only <span class="token punctuation">[</span>alias<span class="token punctuation">]</span>/<span class="token punctuation">[</span>branch<span class="token punctuation">]</span>
</span></code></pre>
<p>将本地分支提交传输到远程存储库分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> push <span class="token punctuation">[</span>alias<span class="token punctuation">]</span> <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>
</span></code></pre>
<p>从跟踪远程分支获取并合并任何提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> pull
</span></code></pre>
<p>将另一个分支的一个特定提交合并到当前分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> cherry-pick <span class="token punctuation">[</span>commit_id<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="远程"><a aria-hidden="true" tabindex="-1" href="#远程"><span class="icon icon-link"></span></a>远程</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>添加一个 git URL 作为别名</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> remote <span class="token function">add</span> <span class="token punctuation">[</span>alias<span class="token punctuation">]</span> <span class="token punctuation">[</span>url<span class="token punctuation">]</span>
</span></code></pre>
<p>显示您设置的远程存储库的名称</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> remote
</span></code></pre>
<p>显示远程存储库的名称和 URL</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> remote <span class="token parameter variable">-v</span>
</span></code></pre>
<p>删除远程存储库</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> remote <span class="token function">rm</span> <span class="token punctuation">[</span>remote repo name<span class="token punctuation">]</span>
</span></code></pre>
<p>更改 git repo 的 URL</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> remote set-url origin <span class="token punctuation">[</span>git_url<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="跟踪路径更改"><a aria-hidden="true" tabindex="-1" href="#跟踪路径更改"><span class="icon icon-link"></span></a>跟踪路径更改</h3><div class="wrap-body">
<p>从项目中删除文件并暂存删除以进行提交</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">rm</span> <span class="token punctuation">[</span>file<span class="token punctuation">]</span>
</span></code></pre>
<p>更改现有文件路径并暂存移动</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">mv</span> <span class="token punctuation">[</span>existing-path<span class="token punctuation">]</span> <span class="token punctuation">[</span>new-path<span class="token punctuation">]</span>
</span></code></pre>
<p>显示所有提交日志，并指示任何移动的路径</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log <span class="token parameter variable">--stat</span> <span class="token parameter variable">-M</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="git-配置-ssh-代理"><a aria-hidden="true" tabindex="-1" href="#git-配置-ssh-代理"><span class="icon icon-link"></span></a>git 配置 ssh 代理</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">cat</span> ~/.ssh/config
</span><span class="code-line">Host gitlab.com
</span><span class="code-line"><span class="token comment"># 直接使用 shadowsocks 提供的 socks5 代理端口</span>
</span><span class="code-line">ProxyCommand <span class="token function">nc</span> <span class="token parameter variable">-X</span> <span class="token number">5</span> <span class="token parameter variable">-x</span> <span class="token number">127.0</span>.0.1:1080 %h %p 
</span><span class="code-line">
</span><span class="code-line">Host github.com
</span><span class="code-line">ProxyCommand <span class="token function">nc</span> <span class="token parameter variable">-X</span> <span class="token number">5</span> <span class="token parameter variable">-x</span> <span class="token number">127.0</span>.0.1:1080 %h %p    
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="gitattributes"><a aria-hidden="true" tabindex="-1" href="#gitattributes"><span class="icon icon-link"></span></a>.gitattributes</h3><div class="wrap-body">
<pre class="language-ini"><code class="language-ini code-highlight"><span class="code-line"><span class="token comment"># 设置默认行为，以防人们没有设置 core.autocrlf</span>
</span><span class="code-line"><span class="token key attr-name">* text</span><span class="token punctuation">=</span><span class="token value attr-value">auto</span>
</span><span class="code-line"><span class="token comment"># 明确声明您希望始终规范化并在结帐时</span>
</span><span class="code-line"><span class="token comment"># 转换为本机行结尾的文本文件</span>
</span><span class="code-line">*.c text
</span><span class="code-line">*.h text
</span><span class="code-line"><span class="token comment"># 声明在结帐时始终以 CRLF 行结尾的文件</span>
</span><span class="code-line"><span class="token key attr-name">*.sln text eol</span><span class="token punctuation">=</span><span class="token value attr-value">crlf</span>
</span><span class="code-line"><span class="token comment"># 表示所有真正二进制且不应修改的文件</span>
</span><span class="code-line">*.png binary
</span><span class="code-line">*.jpg binary
</span></code></pre>
<p><a href="https://github.com/github/linguist/blob/master/docs/overrides.md#using-gitattributes">计入存储库语言</a></p>
<pre class="language-ini"><code class="language-ini code-highlight"><span class="code-line"><span class="token comment"># 标记或取消标记要根据存储库的语言统计数据而</span>
</span><span class="code-line"><span class="token comment"># 忽略或默认隐藏差异的路径</span>
</span><span class="code-line"><span class="token key attr-name">search/index.json linguist-generated</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
</span><span class="code-line"><span class="token comment"># 以下属性统计 SQL 文件</span>
</span><span class="code-line"><span class="token key attr-name">*.sql linguist-detectable</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
</span><span class="code-line"><span class="token comment"># 从统计信息中排除</span>
</span><span class="code-line"><span class="token key attr-name">docs/formatter.rb linguist-documentation</span><span class="token punctuation">=</span><span class="token value attr-value">false</span>
</span><span class="code-line"><span class="token comment"># 将它们从统计信息中排除</span>
</span><span class="code-line">special-vendored-path/* linguist-vendored
</span><span class="code-line"><span class="token comment"># 将所有 .rb 文件检测为 Java 文件</span>
</span><span class="code-line"><span class="token key attr-name">*.rb linguist-language</span><span class="token punctuation">=</span><span class="token value attr-value">Java</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="git-技巧"><a aria-hidden="true" tabindex="-1" href="#git-技巧"><span class="icon icon-link"></span></a>Git 技巧</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重命名分支"><a aria-hidden="true" tabindex="-1" href="#重命名分支"><span class="icon icon-link"></span></a>重命名分支</h3><div class="wrap-body">
<ul class="style-timeline">
<li>
<p><strong>重命名</strong>为<code>new</code></p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-m</span> <span class="token operator">&#x3C;</span>new<span class="token operator">></span>
</span><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-m</span> <span class="token operator">&#x3C;</span>old<span class="token operator">></span> <span class="token operator">&#x3C;</span>new<span class="token operator">></span> <span class="token comment">#重命名分支  </span>
</span></code></pre>
</li>
<li>
<p><strong>推送</strong>并重置</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> push origin <span class="token parameter variable">-u</span> <span class="token operator">&#x3C;</span>new<span class="token operator">></span>
</span></code></pre>
</li>
<li>
<p><strong>删除</strong>远程分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> push origin <span class="token parameter variable">--delete</span> <span class="token operator">&#x3C;</span>old<span class="token operator">></span> <span class="token comment">#方法1</span>
</span><span class="code-line">$ <span class="token function">git</span> push origin :oldBranchName <span class="token comment">#方法2</span>
</span></code></pre>
</li>
</ul>
<!--rehype:className=style-timeline-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="log"><a aria-hidden="true" tabindex="-1" href="#log"><span class="icon icon-link"></span></a>Log</h3><div class="wrap-body">
<p>按内容搜索更改</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log -S<span class="token string">'&#x3C;a term in the source>'</span>
</span></code></pre>
<p>显示特定文件随时间的变化</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log <span class="token parameter variable">-p</span> <span class="token operator">&#x3C;</span>file_name<span class="token operator">></span>
</span></code></pre>
<p>打印出很酷的日志可视化</p>
<pre class="wrap-text"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> log <span class="token parameter variable">--pretty</span><span class="token operator">=</span>oneline <span class="token parameter variable">--graph</span> <span class="token parameter variable">--decorate</span> <span class="token parameter variable">--all</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="分支"><a aria-hidden="true" tabindex="-1" href="#分支"><span class="icon icon-link"></span></a>分支</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>列出所有分支及其上游</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-vv</span> 
</span></code></pre>
<p>快速切换到上一个分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout -
</span></code></pre>
<p>只获取所有远程分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-r</span>
</span></code></pre>
<p>从另一个分支签出单个文件</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout <span class="token operator">&#x3C;</span>branch<span class="token operator">></span> -- <span class="token operator">&#x3C;</span>file<span class="token operator">></span>
</span></code></pre>
<p>删除本地存在远程不存在的分支</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">git</span> remote prune origin
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="commit"><a aria-hidden="true" tabindex="-1" href="#commit"><span class="icon icon-link"></span></a>Commit</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> commit <span class="token parameter variable">-v</span> <span class="token parameter variable">--amend</span>
</span></code></pre>
<p>重写最后的提交信息</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="忽略文件的权限变化"><a aria-hidden="true" tabindex="-1" href="#忽略文件的权限变化"><span class="icon icon-link"></span></a>忽略文件的权限变化</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token function">git</span> config core.fileMode <span class="token boolean">false</span>
</span></code></pre>
<p>不再将文件的权限变化视作改动</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="git-别名"><a aria-hidden="true" tabindex="-1" href="#git-别名"><span class="icon icon-link"></span></a>Git 别名</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.co checkout
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.br branch
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.ci commit
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.st status
</span></code></pre>
<p>也可以看看：<a href="https://gist.github.com/johnpolacek/69604a1f6861129ef088">更多别名</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="设置大小写敏感"><a aria-hidden="true" tabindex="-1" href="#设置大小写敏感"><span class="icon icon-link"></span></a>设置大小写敏感</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 查看git 的设置</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--get</span> core.ignorecase
</span><span class="code-line"><span class="token comment"># 设置大小写敏感</span>
</span><span class="code-line">$ <span class="token function">git</span> config core.ignorecase <span class="token boolean">false</span>
</span><span class="code-line"><span class="token comment"># 远程有俩相同目录，通过这种方式清除掉，然后提交记录</span>
</span><span class="code-line">$ <span class="token function">git</span> <span class="token function">rm</span> <span class="token parameter variable">-r</span> <span class="token parameter variable">--cached</span> <span class="token operator">&#x3C;</span>目录/文件<span class="token operator">></span> 
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="修改远程-commit-记录"><a aria-hidden="true" tabindex="-1" href="#修改远程-commit-记录"><span class="icon icon-link"></span></a>修改远程 Commit 记录</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> rebase <span class="token parameter variable">-i</span> HEAD~3
</span><span class="code-line"><span class="token comment"># 表示要修改当前版本的倒数第三次状态</span>
</span><span class="code-line"><span class="token comment"># 将要更改的记录行首单词 pick 改为 edit</span>
</span><span class="code-line">pick 96dc3f9 提交 commit 描述内容 <span class="token number">1</span>
</span><span class="code-line">pick f1cce8a 提交 commit 描述内容 <span class="token number">2</span>
</span><span class="code-line">pick <span class="token number">6293516</span> 提交 commit 描述内容 <span class="token number">3</span>
</span><span class="code-line"><span class="token comment"># Rebase eeb03a4..6293516 onto eeb03a4 </span>
</span><span class="code-line"><span class="token comment">#                     (3 commands)</span>
</span><span class="code-line"><span class="token comment">#</span>
</span><span class="code-line"><span class="token comment"># Commands:</span>
</span><span class="code-line"><span class="token comment"># p, pick = 使用提交</span>
</span><span class="code-line"><span class="token comment"># r, reword = 使用提交，但编辑提交消息</span>
</span><span class="code-line"><span class="token comment"># e, edit = 使用提交，但停止修改</span>
</span><span class="code-line"><span class="token comment"># s, squash = 使用提交，但融合到先前的提交中</span>
</span><span class="code-line"><span class="token comment"># f, fixup = 像 squash，但丢弃此提交的日志消息</span>
</span><span class="code-line"><span class="token comment"># x, exec = 使用 shell 运行命令(该行的其余部分)</span>
</span><span class="code-line"><span class="token comment"># d, drop = 删除提交</span>
</span></code></pre>
<p>保存并退出，会弹出下面提示</p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 您现在可以修改提交，使用</span>
</span><span class="code-line"><span class="token comment"># </span>
</span><span class="code-line"><span class="token comment">#   git commit --amend</span>
</span><span class="code-line"><span class="token comment"># </span>
</span><span class="code-line"><span class="token comment"># 对更改感到满意后，运行</span>
</span><span class="code-line"><span class="token comment"># </span>
</span><span class="code-line"><span class="token comment">#   git rebase --continue</span>
</span><span class="code-line"><span class="token comment">#</span>
</span><span class="code-line"><span class="token comment"># 1. 通过这条命令进入编辑更改 commit，保存退出</span>
</span><span class="code-line">$ <span class="token function">git</span> commit <span class="token parameter variable">--amend</span>
</span><span class="code-line"><span class="token comment"># 2. 保存退出确认修改，继续执行下面命令, </span>
</span><span class="code-line">$ <span class="token function">git</span> rebase <span class="token parameter variable">--continue</span>
</span><span class="code-line"><span class="token comment"># 如果修改多条记录反复执行上面两条命令直到完成所有修改</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 最后，确保没有人提交进行推送，最好不要加 -f 强制推送</span>
</span><span class="code-line">$ <span class="token function">git</span> push <span class="token parameter variable">-f</span> origin master
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="撤销远程记录"><a aria-hidden="true" tabindex="-1" href="#撤销远程记录"><span class="icon icon-link"></span></a>撤销远程记录</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 撤销一条记录   </span>
</span><span class="code-line">$ <span class="token function">git</span> reset <span class="token parameter variable">--hard</span> HEAD~1
</span><span class="code-line"><span class="token comment"># 强制同步到远程仓库  </span>
</span><span class="code-line">$ <span class="token function">git</span> push <span class="token parameter variable">-f</span> origin HEAD:master
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="放弃本地修改内容"><a aria-hidden="true" tabindex="-1" href="#放弃本地修改内容"><span class="icon icon-link"></span></a>放弃本地修改内容</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 如果有的修改以及加入暂存区的话</span>
</span><span class="code-line">$ <span class="token function">git</span> reset <span class="token parameter variable">--hard</span> 
</span><span class="code-line"><span class="token comment"># 还原所有修改，不会删除新增的文件</span>
</span><span class="code-line">$ <span class="token function">git</span> checkout <span class="token builtin class-name">.</span> 
</span><span class="code-line"><span class="token comment"># 下面命令会删除新增的文件</span>
</span><span class="code-line">$ <span class="token function">git</span> clean <span class="token parameter variable">-xdf</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取最近一次提交的-hash"><a aria-hidden="true" tabindex="-1" href="#获取最近一次提交的-hash"><span class="icon icon-link"></span></a>获取最近一次提交的 Hash</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> rev-parse HEAD <span class="token comment"># e10721cb8859b2c</span>
</span><span class="code-line"><span class="token comment"># 获取短 hash</span>
</span><span class="code-line">$ <span class="token function">git</span> rev-parse <span class="token parameter variable">--short</span> HEAD <span class="token comment"># e10721c</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除已经合并到-master-的分支"><a aria-hidden="true" tabindex="-1" href="#删除已经合并到-master-的分支"><span class="icon icon-link"></span></a>删除已经合并到 master 的分支</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">--merged</span> master <span class="token operator">|</span> <span class="token function">grep</span> <span class="token parameter variable">-v</span> <span class="token string">'^\*\|  master'</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token parameter variable">-n</span> <span class="token number">1</span> <span class="token function">git</span> branch <span class="token parameter variable">-d</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="把-a-分支的某一个-commit放到-b-分支上"><a aria-hidden="true" tabindex="-1" href="#把-a-分支的某一个-commit放到-b-分支上"><span class="icon icon-link"></span></a>把 A 分支的某一个 commit，放到 B 分支上</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 切换到 B 分支</span>
</span><span class="code-line">$ <span class="token function">git</span> checkout <span class="token operator">&#x3C;</span>B<span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 将 A 分支 &#x3C;hash-id> 的内容 pick 到 B 分支</span>
</span><span class="code-line">$ <span class="token function">git</span> cherry-pick <span class="token operator">&#x3C;</span>hash-id<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="回到远程仓库的状态"><a aria-hidden="true" tabindex="-1" href="#回到远程仓库的状态"><span class="icon icon-link"></span></a>回到远程仓库的状态</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> fetch <span class="token parameter variable">--all</span> <span class="token operator">&#x26;&#x26;</span> <span class="token function">git</span> reset <span class="token parameter variable">--hard</span> origin/master
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>抛弃本地所有的修改，回到远程仓库的状态</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重设第一个-commit"><a aria-hidden="true" tabindex="-1" href="#重设第一个-commit"><span class="icon icon-link"></span></a>重设第一个 commit</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> update-ref <span class="token parameter variable">-d</span> HEAD
</span></code></pre>
<p>把所有的改动都重新放回工作区，并<strong>清空所有的 commit</strong>，这样就可以重新提交第一个 <code>commit</code> 了</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="查看冲突文件列表"><a aria-hidden="true" tabindex="-1" href="#查看冲突文件列表"><span class="icon icon-link"></span></a>查看冲突文件列表</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> --name-only --diff-filter<span class="token operator">=</span>U
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="展示工作区的冲突文件列表"><a aria-hidden="true" tabindex="-1" href="#展示工作区的冲突文件列表"><span class="icon icon-link"></span></a>展示工作区的冲突文件列表</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>输出工作区和暂存区的 different (不同)。</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span>
</span></code></pre>
<p>还可以展示本地仓库中任意两个 commit 之间的文件变动：</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> <span class="token operator">&#x3C;</span>commit-id<span class="token operator">></span> <span class="token operator">&#x3C;</span>commit-id<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示暂存区和最近版本的不同"><a aria-hidden="true" tabindex="-1" href="#展示暂存区和最近版本的不同"><span class="icon icon-link"></span></a>展示暂存区和最近版本的不同</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> <span class="token function">diff</span> <span class="token parameter variable">--cached</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="中文乱码的解决方案"><a aria-hidden="true" tabindex="-1" href="#中文乱码的解决方案"><span class="icon icon-link"></span></a>中文乱码的解决方案</h3><div class="wrap-body">
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> core.quotepath <span class="token boolean">false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示暂存区工作区和最近版本的不同"><a aria-hidden="true" tabindex="-1" href="#展示暂存区工作区和最近版本的不同"><span class="icon icon-link"></span></a>展示暂存区、工作区和最近版本的不同</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> HEAD
</span></code></pre>
<p>输出工作区、暂存区 和本地最近的版本(commit)的different(不同)。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除已经合并到-master-的分支-1"><a aria-hidden="true" tabindex="-1" href="#删除已经合并到-master-的分支-1"><span class="icon icon-link"></span></a>删除已经合并到 master 的分支</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">--merged</span> master <span class="token operator">|</span> <span class="token function">grep</span> <span class="token parameter variable">-v</span> <span class="token string">'^\*\|  master'</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token parameter variable">-n</span> <span class="token number">1</span> <span class="token function">git</span> branch <span class="token parameter variable">-d</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="关联远程分支"><a aria-hidden="true" tabindex="-1" href="#关联远程分支"><span class="icon icon-link"></span></a>关联远程分支</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> branch <span class="token parameter variable">-u</span> origin/mybranch
</span></code></pre>
<p>或者在 <code>push</code> 时加上 <code>-u</code> 参数</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> push origin/mybranch <span class="token parameter variable">-u</span>
</span></code></pre>
<p>关联之后，<code>git branch -vv</code> 就可以展示关联的远程分支名了, 同时推送到远程仓库直接：<code>git push</code>，不需要指定远程仓库</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="查看远程分支和本地分支的对应关系"><a aria-hidden="true" tabindex="-1" href="#查看远程分支和本地分支的对应关系"><span class="icon icon-link"></span></a>查看远程分支和本地分支的对应关系</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> remote show origin
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示当前分支的最近的-tag"><a aria-hidden="true" tabindex="-1" href="#展示当前分支的最近的-tag"><span class="icon icon-link"></span></a>展示当前分支的最近的 tag</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> describe <span class="token parameter variable">--tags</span> <span class="token parameter variable">--abbrev</span><span class="token operator">=</span><span class="token number">0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="查看某段代码是谁写的"><a aria-hidden="true" tabindex="-1" href="#查看某段代码是谁写的"><span class="icon icon-link"></span></a>查看某段代码是谁写的</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> blame <span class="token operator">&#x3C;</span>file-name<span class="token operator">></span>
</span></code></pre>
<p><code>blame</code> 的意思为<code>责怪</code>，你懂的。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="修改作者名"><a aria-hidden="true" tabindex="-1" href="#修改作者名"><span class="icon icon-link"></span></a>修改作者名</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> commit <span class="token parameter variable">--amend</span> <span class="token parameter variable">--author</span><span class="token operator">=</span><span class="token string">'Author Name &#x3C;email@address.com>'</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="修改远程仓库的-url"><a aria-hidden="true" tabindex="-1" href="#修改远程仓库的-url"><span class="icon icon-link"></span></a>修改远程仓库的 url</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> remote set-url origin <span class="token operator">&#x3C;</span>URL<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="增加远程仓库"><a aria-hidden="true" tabindex="-1" href="#增加远程仓库"><span class="icon icon-link"></span></a>增加远程仓库</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> remote <span class="token function">add</span> origin <span class="token operator">&#x3C;</span>remote-url<span class="token operator">></span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列出所有远程仓库"><a aria-hidden="true" tabindex="-1" href="#列出所有远程仓库"><span class="icon icon-link"></span></a>列出所有远程仓库</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> remote <span class="token parameter variable">-v</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="查看两个星期内的改动"><a aria-hidden="true" tabindex="-1" href="#查看两个星期内的改动"><span class="icon icon-link"></span></a>查看两个星期内的改动</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> whatchanged <span class="token parameter variable">--since</span><span class="token operator">=</span><span class="token string">'2 weeks ago'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从-stash-中拿出某个文件的修改"><a aria-hidden="true" tabindex="-1" href="#从-stash-中拿出某个文件的修改"><span class="icon icon-link"></span></a>从 stash 中拿出某个文件的修改</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout <span class="token operator">&#x3C;</span>stash@<span class="token punctuation">{</span>n<span class="token punctuation">}</span><span class="token operator">></span> -- <span class="token operator">&#x3C;</span>file-path<span class="token operator">></span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示所有-tracked-的文件"><a aria-hidden="true" tabindex="-1" href="#展示所有-tracked-的文件"><span class="icon icon-link"></span></a>展示所有 tracked 的文件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> ls-files <span class="token parameter variable">-t</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示所有-untracked-的文件"><a aria-hidden="true" tabindex="-1" href="#展示所有-untracked-的文件"><span class="icon icon-link"></span></a>展示所有 untracked 的文件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> ls-files <span class="token parameter variable">--others</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示所有忽略的文件"><a aria-hidden="true" tabindex="-1" href="#展示所有忽略的文件"><span class="icon icon-link"></span></a>展示所有忽略的文件</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> ls-files <span class="token parameter variable">--others</span> <span class="token parameter variable">-i</span> --exclude-standard
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="把某一个分支导出成一个文件"><a aria-hidden="true" tabindex="-1" href="#把某一个分支导出成一个文件"><span class="icon icon-link"></span></a>把某一个分支导出成一个文件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> bundle create <span class="token operator">&#x3C;</span>file<span class="token operator">></span> <span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="从包中导入分支"><a aria-hidden="true" tabindex="-1" href="#从包中导入分支"><span class="icon icon-link"></span></a>从包中导入分支</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> clone repo.bundle <span class="token operator">&#x3C;</span>repo-dir<span class="token operator">></span> <span class="token parameter variable">-b</span> <span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>新建一个分支，分支内容就是上面 <code>git bundle create</code> 命令导出的内容</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="执行-rebase-之前自动-stash"><a aria-hidden="true" tabindex="-1" href="#执行-rebase-之前自动-stash"><span class="icon icon-link"></span></a>执行 rebase 之前自动 stash</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> rebase <span class="token parameter variable">--autostash</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从远程仓库根据-id拉下某一状态到本地分支"><a aria-hidden="true" tabindex="-1" href="#从远程仓库根据-id拉下某一状态到本地分支"><span class="icon icon-link"></span></a>从远程仓库根据 ID，拉下某一状态，到本地分支</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> fetch origin pull/<span class="token operator">&#x3C;</span>id<span class="token operator">></span>/head:<span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="详细展示一行中的修改"><a aria-hidden="true" tabindex="-1" href="#详细展示一行中的修改"><span class="icon icon-link"></span></a>详细展示一行中的修改</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> <span class="token function">diff</span> --word-diff
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="清除-gitignore-文件中记录的文件"><a aria-hidden="true" tabindex="-1" href="#清除-gitignore-文件中记录的文件"><span class="icon icon-link"></span></a>清除 gitignore 文件中记录的文件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> clean <span class="token parameter variable">-X</span> <span class="token parameter variable">-f</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示忽略的文件"><a aria-hidden="true" tabindex="-1" href="#展示忽略的文件"><span class="icon icon-link"></span></a>展示忽略的文件</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> status <span class="token parameter variable">--ignored</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="commit-历史中显示-branch1-有的但是-branch2-没有-commit"><a aria-hidden="true" tabindex="-1" href="#commit-历史中显示-branch1-有的但是-branch2-没有-commit"><span class="icon icon-link"></span></a>commit 历史中显示 Branch1 有的但是 Branch2 没有 commit</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> log Branch1 ^Branch2
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在-commit-log-中显示-gpg-签名"><a aria-hidden="true" tabindex="-1" href="#在-commit-log-中显示-gpg-签名"><span class="icon icon-link"></span></a>在 commit log 中显示 GPG 签名</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> log --show-signature
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="新建并切换到新分支上同时这个分支没有任何-commit"><a aria-hidden="true" tabindex="-1" href="#新建并切换到新分支上同时这个分支没有任何-commit"><span class="icon icon-link"></span></a>新建并切换到新分支上，同时这个分支没有任何 commit</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> checkout <span class="token parameter variable">--orphan</span> <span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>
</span></code></pre>
<p>相当于保存修改，但是重写 commit 历史</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="展示任意分支某一文件的内容"><a aria-hidden="true" tabindex="-1" href="#展示任意分支某一文件的内容"><span class="icon icon-link"></span></a>展示任意分支某一文件的内容</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> show <span class="token operator">&#x3C;</span>branch-name<span class="token operator">></span>:<span class="token operator">&#x3C;</span>file-name<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="配置-http-和-socks-代理"><a aria-hidden="true" tabindex="-1" href="#配置-http-和-socks-代理"><span class="icon icon-link"></span></a>配置 http 和 socks 代理</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 查看代理</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> http.proxy
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> https.proxy
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> socks.proxy
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 设置代理</span>
</span><span class="code-line"><span class="token comment"># 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> http.proxy http://127.0.0.1:1080
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> https.proxy http://127.0.0.1:1080
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> socks.proxy <span class="token number">127.0</span>.0.1:1080
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 取消代理</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> http.proxy
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> https.proxy
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> socks.proxy
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 只对 github.com 设置代理</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> http.https://github.com.proxy socks5://127.0.0.1:1080
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> https.https://github.com.proxy socks5://127.0.0.1:1080
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 取消 github.com 代理</span>
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> http.https://github.com.proxy
</span><span class="code-line">$ <span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> https.https://github.com.proxy
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="clone-最新一次提交"><a aria-hidden="true" tabindex="-1" href="#clone-最新一次提交"><span class="icon icon-link"></span></a>clone 最新一次提交</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">git</span> clone <span class="token parameter variable">--depth</span><span class="token operator">=</span><span class="token number">1</span> https://github.com/user/repo.git
</span></code></pre>
<p>只会 <code>clone</code> 最近一次提交，将减少 <code>clone</code> 时间</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="忽略某个文件的改动"><a aria-hidden="true" tabindex="-1" href="#忽略某个文件的改动"><span class="icon icon-link"></span></a>忽略某个文件的改动</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>关闭 track 指定文件的改动，也就是 Git 将不会在记录这个文件的改动</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> update-index --assume-unchanged path/to/file
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>恢复 track 指定文件的改动</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> update-index --no-assume-unchanged path/to/file
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="以最后提交的顺序列出所有-git-分支"><a aria-hidden="true" tabindex="-1" href="#以最后提交的顺序列出所有-git-分支"><span class="icon icon-link"></span></a>以最后提交的顺序列出所有 Git 分支</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> for-each-ref <span class="token parameter variable">--sort</span><span class="token operator">=</span>-committerdate <span class="token parameter variable">--format</span><span class="token operator">=</span><span class="token string">'%(refname:short)'</span> refs/heads
</span></code></pre>
<p>最新的放在最上面</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在-commit-log-中查找相关内容"><a aria-hidden="true" tabindex="-1" href="#在-commit-log-中查找相关内容"><span class="icon icon-link"></span></a>在 commit log 中查找相关内容</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> log <span class="token parameter variable">--all</span> <span class="token parameter variable">--grep</span><span class="token operator">=</span><span class="token string">'&#x3C;given-text>'</span>
</span></code></pre>
<p>通过 grep 查找，given-text: 所需要查找的字段</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="把暂存区的指定-file-放到工作区中"><a aria-hidden="true" tabindex="-1" href="#把暂存区的指定-file-放到工作区中"><span class="icon icon-link"></span></a>把暂存区的指定 file 放到工作区中</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">git</span> reset <span class="token operator">&#x3C;</span>file-name<span class="token operator">></span>
</span></code></pre>
<p>不添加参数，默认是 <code>-mixed</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="配置-ssh-协议代理"><a aria-hidden="true" tabindex="-1" href="#配置-ssh-协议代理"><span class="icon icon-link"></span></a>配置 SSH 协议代理</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 对于使用 git@ 协议的，可以配置 socks5 代理</span>
</span><span class="code-line"><span class="token comment"># macOS 系统编辑 ~/.ssh/config 文件，添加这几行，设置 github 代理</span>
</span><span class="code-line">Host github.com
</span><span class="code-line">  ProxyCommand <span class="token function">nc</span> <span class="token parameter variable">-X</span> <span class="token number">5</span> <span class="token parameter variable">-x</span> <span class="token number">127.0</span>.0.1:1080 %h %p
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="conventional-commmits"><a aria-hidden="true" tabindex="-1" href="#conventional-commmits"><span class="icon icon-link"></span></a>Conventional Commmits</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="格式"><a aria-hidden="true" tabindex="-1" href="#格式"><span class="icon icon-link"></span></a>格式</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-text"><code class="language-text code-highlight"><span class="code-line">&#x3C;type>(&#x3C;scope>): &#x3C;short summary>
</span><span class="code-line">  │       │             │
</span><span class="code-line">  │       │             └─⫸ 紧凑简短的描述，无需大写，也不需要用句号结尾
</span><span class="code-line">  │       │
</span><span class="code-line">  │       └─⫸ Commit 范围: animations|bazel|benchpress|common|compiler|compiler-cli|core|
</span><span class="code-line">  │                          elements|forms|http|language-service|localize|platform-browser|
</span><span class="code-line">  │                          platform-browser-dynamic|platform-server|router|service-worker|
</span><span class="code-line">  │                          upgrade|zone.js|packaging|changelog|docs-infra|migrations|ngcc|ve|
</span><span class="code-line">  │                          devtools....
</span><span class="code-line">  │
</span><span class="code-line">  └─⫸ Commit 类型: build|ci|doc|docs|feat|fix|perf|refactor|test
</span><span class="code-line">                    website|chore|style|type|revert
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-1"><div class="wrap-header h3wrap"><h3 id="常用"><a aria-hidden="true" tabindex="-1" href="#常用"><span class="icon icon-link"></span></a>常用</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-1-->

























<table class="left-align"><thead><tr><th>类型</th><th>描述</th></tr></thead><tbody><tr><td><code>feat:</code></td><td>新特性</td></tr><tr><td><code>fix(scope):</code></td><td>修复 scope 中的 Bug</td></tr><tr><td><code>feat!:</code> / <code>feat(scope)!:</code></td><td>breaking change /  重构 API</td></tr><tr><td><code>chore(deps):</code></td><td>更新依赖</td></tr></tbody></table>
<!--rehype:className=left-align-->
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="commit-类型"><a aria-hidden="true" tabindex="-1" href="#commit-类型"><span class="icon icon-link"></span></a>Commit 类型</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->





















































<table class="left-align"><thead><tr><th>类型</th><th>描述</th></tr></thead><tbody><tr><td><code>build:</code></td><td>变更影响的是<strong>构建系统</strong>或者<strong>外部依赖</strong> (如: gulp, npm)</td></tr><tr><td><code>ci:</code></td><td>修改了 CI 配置文件或脚本 (如: Github Action, Travis)</td></tr><tr><td><code>chore:</code></td><td><strong>【重要】</strong> 变更不影响源代码或测试（如更新了辅助工具、库等)</td></tr><tr><td><code>docs:</code></td><td>只修改了文档</td></tr><tr><td><code>feat:</code></td><td><strong>【重要】</strong> 一个新特性</td></tr><tr><td><code>fix:</code></td><td><strong>【重要】</strong> 修复了一个 Bug</td></tr><tr><td><code>perf:</code></td><td>增强性能的代码变更</td></tr><tr><td><code>refactor:</code></td><td>并非修复 Bug 或添加新特性的代码变更</td></tr><tr><td><code>revert:</code></td><td>回退代码</td></tr><tr><td><code>style:</code></td><td>变更不影响一些有意义的代码 (如: 删除空格、格式化代码、添加分号等)</td></tr><tr><td><code>test:</code></td><td>添加测试代码或修正已有的测试</td></tr></tbody></table>
<!--rehype:className=left-align-->
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://github.com/git-tips/tips">最常用的 git 提示和技巧</a></li>
<li><a href="https://www.conventionalcommits.org/zh-hans/v1.0.0/">Conventional Commits 官方网站</a> <em>(conventionalcommits.org)</em></li>
<li><a href="https://gist.github.com/Zekfad/f51cb06ac76e2457f11c80ed705c95a3">Conventional Commits Cheatsheet</a> <em>(gist.github.com)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js" defer></script><script src="../js/fuse.min.js" defer></script><script src="../js/main.js" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
